use chrono::{DateTime, NaiveDateTime, Utc};
use rust_decimal::Decimal;
use sea_orm::ActiveValue;
use serde::{Deserialize, Serialize};

use crate::{
    convert_field,
    entities::{self, order_detail, orders::ActiveModel},
};
#[derive(Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct OrdersSubmitDTO {
    pub address_book_id: i64,
    pub pay_method: i32,
    pub remark: String,
    pub estimated_delivery_time: String,
    pub delivery_status: i8,
    pub tableware_number: i32,
    pub tableware_status: i8,
    pub pack_amount: i32,
    pub amount: Decimal,
}

impl From<OrdersSubmitDTO> for ActiveModel {
    fn from(dto: OrdersSubmitDTO) -> Self {
        ActiveModel {
            address_book_id: convert_field!(dto, address_book_id, i64),
            pay_method: convert_field!(dto, pay_method, i32),
            remark: convert_field!(dto, remark, String).into(),
            estimated_delivery_time: ActiveValue::set(Some(
                chrono::NaiveDateTime::parse_from_str(
                    &dto.estimated_delivery_time,
                    "%Y-%m-%d %H:%M:%S",
                )
                .unwrap(),
            )),
            delivery_status: convert_field!(dto, delivery_status, i8),
            tableware_number: convert_field!(dto, tableware_number, i32).into(),
            tableware_status: convert_field!(dto, tableware_status, i8),
            pack_amount: convert_field!(dto, pack_amount, i32).into(),
            amount: convert_field!(dto, amount, Decimal),
            ..Default::default()
        }
    }
}

#[derive(Debug, Deserialize, Serialize, Clone, Copy)]
#[serde(rename_all = "camelCase")]
pub struct OrderPaymentDTO {
    pub order_number: i64, //订单号
    pub pay_method: i8,    //付款方式
}

#[derive(Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct OrderPaymentVO {
    pub nonce_str: String,   //随机字符串
    pub pay_sign: String,    //签名
    pub time_stamp: String,  //时间戳
    pub sign_type: String,   //签名算法
    pub package_str: String, //统一下单接口返回的 prepay_id 参数值
}

#[derive(Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct OrderPageQueryDTO {
    pub status: Option<i32>,
    // 页码
    pub page: usize,
    // 每页显示记录数
    pub page_size: usize,
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct OrdersPageQueryDTO {
    pub page: usize,                       // 页码
    pub page_size: usize,                  // 每页数据条数
    pub number: Option<String>,            // 订单号
    pub phone: Option<String>,             // 用户电话
    pub status: Option<i32>,               // 订单状态
    pub begin_time: Option<NaiveDateTime>, // 开始时间
    pub end_time: Option<NaiveDateTime>,   // 结束时间
    pub user_id: Option<i64>,              // 用户ID
}

#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
pub struct OrderConfirmDTO {
    pub id: i64,
    // pub status: i32,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct OrderRejectionDTO {
    pub id: i64,
    pub rejection_reason: String,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct OrderCancelDTO {
    pub id: i64,
    pub cancel_reason: String,
}
